package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.common.redis.service.JedisClient;
import com.jlkf.dao.CarCaculationPointChargesMapper;
import com.jlkf.pojo.CarCaculationPointCharges;
import com.jlkf.service.CarCaculationPointChargesService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.RegexUtil;
import com.platform.server.util.StringTools;

import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class CarCaculationPointChargesServiceImpl implements CarCaculationPointChargesService{

	@Autowired
	private CarCaculationPointChargesMapper carCaculationPointChargesMapper;
	@Autowired
	private JedisClient jedisClient;
	
	
	@Override
	public Object getList(String pointChargesName, Integer pageNo, Integer pageSize, Integer type) {
		
		Map<String,Object> map = new HashMap<>();
		if(!StringTools.isEmpty(pointChargesName)){
			map.put("pointChargesName", pointChargesName);
		}
		if(!RegexUtil.isNull(type)){
			map.put("type", type);
		}
		try {
			PageHelper.startPage(pageNo, pageSize);
			List<CarCaculationPointCharges> list = carCaculationPointChargesMapper.getList(map);
			PageInfo<CarCaculationPointCharges> info = new PageInfo<>(list);
			return new ReturnData(list,info.getPages(),(int)info.getTotal(),200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	//删除
	@Override
	public Object del(Long id) {
		try {
			int n = carCaculationPointChargesMapper.deleteByPrimaryKey(id);
			if(n==0){
				return new ReturnData(201,"删除失败");
			}
			return new ReturnData(200,"删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}
	@Autowired
	private JedisPool jedisPool; 
	@Override
	public Object addOrUpdate(String json) {
		CarCaculationPointCharges ccpc=StringTools.json2Obj(json, CarCaculationPointCharges.class);
		if(ccpc==null){
			return new ReturnData(201,"参数列表为空");
		}
		int num=1;
		Jedis jedis = null;
		if(jedis==null){
			jedis=jedisPool.getResource();
		}
		try {
			if(ccpc.getId()!=null){
				 num=carCaculationPointChargesMapper.updateByPrimaryKeySelective(ccpc);
			}else{
				ccpc.setCreateTime(new Date());
			   num=carCaculationPointChargesMapper.insertSelective(ccpc);
			}
			if(num==0){
				return new ReturnData(202,"保存失败");
			}
			Map<String,Object> map = new HashMap<>();
			map.put("type", 1);
			List<CarCaculationPointCharges> list = carCaculationPointChargesMapper.getList(map);
			 JSONArray ja1 = JSONArray.fromObject(list);
			 String newContent = ja1.toString();
			 //carCaculationPointCharges
			 jedis.hset("carCaculationPointCharges",  "carCaculationPointCharges_point", newContent);//新增与修改之后 将最新的收费站信息存到缓存中
			 map.put("type", 2);
			List<CarCaculationPointCharges> list1 = carCaculationPointChargesMapper.getList(map);
			JSONArray ja2 = JSONArray.fromObject(list1);
			 String Content = ja2.toString();
			 jedis.hset("carCaculationPointCharges",  "carCaculationPointCharges_line", Content);//新增与修改之后 将最新的收费站信息存到缓存中
			return new ReturnData(200,"保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}finally {
			 if(jedis!=null){
				 jedis.close();
			 }
		}
	}

}
